package stu.day1_day8;

import java.util.ArrayDeque;

public class LeetCode224 {

    public int calculate(String s) {
        ArrayDeque<Integer> stack = new ArrayDeque<Integer>();

          int len = s.length();
          int num = 0;   //数值
          int index = 1; //+ -

          for(int i = 0;i<len;i++){

              char ch = s.charAt(i);

              if(Character.isDigit(ch)){
                  int value  = ch - '0';
                  while (i+1<len && Character.isDigit(s.charAt(i+1))){
                      i += 1;
                      int va = s.charAt(i) - '0';
                      value = value*10 + va;
                  }
                  num = num + index*value;
              }else if(ch == '+'){
                  index = 1;
              }else if(ch == '-'){
                  index = -1;
              }else if(ch == '('){
                  stack.push(num);
                  stack.push(index);
                  num = 0;
                  index = 1;
              }else if(ch == ')'){
                  int stIndex = stack.pop();
                  int stNum = stack.pop();

                  num = stNum + stIndex*num;
              }

          }

        return num;
    }

    public static void main(String[] args) {

    }


}
